package com.csk.zsk.compents.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.csk.zsk.commons.action.AjaxActionSupport;
import com.csk.zsk.commons.utils.encode.JsonBinder;
import com.csk.zsk.commons.utils.reflection.ConvertUtils;
import com.csk.zsk.commons.utils.web.struts2.Struts2Utils;
import com.csk.zsk.compents.entity.Personcollection;
import com.csk.zsk.compents.entity.User;
import com.csk.zsk.compents.service.PersoncollectionService;
import com.csk.zsk.compents.service.VideoService;
import com.csk.zsk.compents.utils.Constants;
import com.google.common.collect.Lists;

@Controller
@Scope("prototype")
public class PersoncollectionAction extends AjaxActionSupport {

	private static final long serialVersionUID = 1733082916462526224L;
	private static JsonBinder binder = JsonBinder.buildNonDefaultBinder();

	@Autowired
	private PersoncollectionService personcollectionService;
	@Autowired
	private VideoService videoService;

	private String objid;
	private String title;
	private String desc;
	private String likeName;
	private String type;

	public void addCollection() {
		User user = (User) Struts2Utils.getSessionAttribute(Constants.USER_SESSION);
		String userid = user.getId();
		try {
			Personcollection personcollection = new Personcollection();
			personcollection.setObjid(objid);
			personcollection.setTitle(title);
			personcollection.setDescription(desc);
			personcollection.setUserid(userid);
			personcollection.setType(type);
			personcollectionService.savePersoncollection(personcollection);
			Struts2Utils.renderText("success");
		} catch (Exception e) {
			Struts2Utils.renderText("fail");
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void searchCollection() {
		User user = (User) Struts2Utils.getSessionAttribute(Constants.USER_SESSION);
		String userid = user.getId();
		List objList = Lists.newArrayList();
		if (likeName == null || likeName.equals("")) {
			return;
		}
		if (type == null || type.equals("")) {
			return;
		}

		List<Personcollection> personcollectionList = personcollectionService.searchPersoncollection(likeName, userid);
		List<String> objidList = ConvertUtils.convertElementPropertyToList(personcollectionList, "objid");
		if (type.equals("1") || type.equals("2") || type.equals("3") || type.equals("4")) {
			objList = videoService.getVideoByIdList(objidList);
		}
		String json = binder.toJson(objList);
		Struts2Utils.renderJson(json);
	}

	public void setObjid(String objid) {
		this.objid = objid;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public void setLikeName(String likeName) {
		this.likeName = likeName;
	}

	public void setType(String type) {
		this.type = type;
	}

}
